package com.changgou.goods.controller;


import com.changgou.common.constant.StatusCode;
import com.changgou.common.exception.BaseExceptionHandler;
import com.changgou.common.vo.Result;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.sql.Statement;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private BaseExceptionHandler baseExceptionHandler;

    /**
         * 查询全部品牌
         * @return 查询结果
         */
    @GetMapping
    public Result<List<Brand>> findAll(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<Brand> brandList = brandService.findAll();
        return new Result<>("查询成功！",brandList);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id")Integer id){
        Brand brand = brandService.findById(id);
        return new Result<>("查询成功！",brand);
    }

    /**
     * 新增数据
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK,"添加成功！");
    }

    /**
     * 更新数据
     * @param brand
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Brand brand){
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功！");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
            brandService.dalete(id);
        return new Result(true,StatusCode.OK,"删除成功!");
    }

    /**
     * 列表条件查询
     */
    @PostMapping("/search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        List<Brand> brandList = brandService.findList(brand);
        return new Result<>("查询成功！",brandList);
    }

    /**
     * 列表分页查询
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page,@PathVariable Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>("查询成功！",pageInfo);
    }

    /**
     * 列表条件分页查询
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody(required = false) Brand brand,@PathVariable Integer page,@PathVariable Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result<PageInfo<Brand>>("查询成功！",pageInfo);
    }
}
